package ranab.server.ftp.gui;

import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;

import ranab.gui.GuiUtils;
import ranab.server.ftp.FtpUser;
import ranab.server.ftp.FtpConfig;
import ranab.server.ftp.ConnectionService;
import ranab.server.ftp.SpyConnectionInterface;


/**
 * This panel is used to monitor user activity.
 *
 * @author <a href="mailto:rana_b@yahoo.com">Rana Bhattacharyya</a>
 */
public 
class SpyPanel extends JPanel implements SpyConnectionInterface {
    
    private JTextArea mLogTxt   = null;
    private FtpConfig mConfig   = null;
    private String mstSessionId = null;
    
    /**
     * Instantiate this dialog box
     */
    public SpyPanel(FtpConfig config, String sessId) {
        mConfig = config;
        mstSessionId = sessId;
        initComponents();
    }
    
    /**
     * Initialize the UI components
     */
    private void initComponents() {
        setLayout(new BorderLayout());
        mLogTxt = new JTextArea();
        mLogTxt.setEditable(false);
        JScrollPane txtPane = new JScrollPane(mLogTxt, 
                    JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                    JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        add(txtPane, BorderLayout.CENTER);
        
        ConnectionService conService = mConfig.getConnectionService();
        if (conService != null) {
            conService.setSpyObject(mstSessionId, this);
        }
    }
    
    /**
     * Get connection session id. 
     */
    public String getSessionId() {
        return mstSessionId;
    }
    
    /**
     * Write server response.
     */
    public void response(String msg) {
        mLogTxt.append(msg);
    }
    
    /**
     * Write user request.
     */
    public void request(String msg) {
        mLogTxt.append(msg);
    }
    
    /**
     * Clear log messages
     */
    public void clearLog() {
        mLogTxt.setText("");
    }
    
    /**
     * Close pane
     */
    public void closePane() {
        ConnectionService conService = mConfig.getConnectionService();
        if (conService != null) {
            conService.setSpyObject(mstSessionId, null);
        }
        mLogTxt.setText("");
    }
    
    /**
     * Disconnect user
     */
    public void disconnect() {
        ConnectionService conService = mConfig.getConnectionService();
        if (conService != null) {
            conService.setSpyObject(mstSessionId, null);
            conService.closeConnection(mstSessionId);
        }
    }
    
}